﻿using System;
using Foothill.Model;

namespace Foothill.Service.AdService
{
    public class MileageStatsGroupManager : IStatsGroupManager
    {
        // determine group Id and name
        public Tuple<int, string> DetermineGroup(ForSaleAd post)
        {
            Tuple<int, string> result = new Tuple<int, string>((int)MileageStatsGroups.MoreThan100K, "10万以上");

            int mileAge = post.MileAge;

            if (mileAge <= 5000)
            {
                result = new Tuple<int, string>((int)MileageStatsGroups.LessThan5K, "小于5千");
            }
            else if (mileAge > 5000 && mileAge <= 20000)
            {
                result = new Tuple<int, string>((int)MileageStatsGroups.FiveKto20K, "5千 到 2万");
            }
            else if (mileAge > 20000 && mileAge <= 50000)
            {
                result = new Tuple<int, string>((int)MileageStatsGroups.TwentyKto50K, "2万 到 5万");
            }
            else if (mileAge > 50000 && mileAge <= 100000)
            {
                result = new Tuple<int, string>((int)MileageStatsGroups.FiveKto100K, "5万 到 10万");
            }
            else
            {
                result = new Tuple<int, string>((int)MileageStatsGroups.MoreThan100K, "10万以上");
            }

            return result;
        }

        public Func<ForSaleAd, bool> GetGroupFunc(int groupId)
        {
            Func<ForSaleAd, bool> func;
            MileageStatsGroups mileageGroup = GetMileageStatsGroupEnum(groupId);

            switch (mileageGroup)
            {
                case MileageStatsGroups.LessThan5K:
                    func = c => c.MileAge <= 5000;
                    break;
                case MileageStatsGroups.FiveKto20K:
                    func = c => c.MileAge > 5000 && c.MileAge <= 20000;
                    break;
                case MileageStatsGroups.TwentyKto50K:
                    func = c => c.MileAge > 20000 && c.MileAge <= 50000;
                    break;
                case MileageStatsGroups.FiveKto100K:
                    func = c => c.MileAge > 50000 && c.MileAge <= 100000;
                    break;
                default:
                    func = c => c.MileAge > 100000;
                    break;
            }

            return func;
        }

        internal MileageStatsGroups GetMileageStatsGroupEnum(int groupId)
        {
            MileageStatsGroups mileageGroup = MileageStatsGroups.TwentyKto50K;

            if (Enum.IsDefined(typeof(MileageStatsGroups), groupId))
            {
                mileageGroup = (MileageStatsGroups)groupId;
            }

            return mileageGroup;
        }
    }
}
